package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;

import bean.Stock;

public class Actionservlet extends HttpServlet {

	@Override
	protected void service(
			HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {

		String uri = req.getRequestURI();
		System.out.println("uri"+uri);
		
		String path = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
		System.out.println("path"+path);
		
		res.setContentType("text/html;charset=utf-8");
		PrintWriter w = res.getWriter();
		if("/quoto".equals(path)){
			//模拟生成机制股票的信息
			//(会以json字符串的形式返回)
			
			List<Stock> stocks = new ArrayList<Stock>();
			Random ran=new Random();
			for (int i = 0; i < 8; i++) {

				Stock s = new Stock();
				s.setCode("60088" + ran.nextInt(10));
				s.setName("中国嘉陵" + ran.nextInt(100));
				s.setPrice(ran.nextInt(1000));
				stocks.add(s);
				
			}

			ObjectMapper om = new ObjectMapper();
			String data = om.writeValueAsString(stocks);
			System.out.println(data);

			w.println(data);
		}else if("/getPrice".equals(path)){
			//读取航班号
			String flight = req.getParameter("flight");
			//依据航班号返回对应的各个航班的价格
			if("CA2201".equals(flight)){
				w.println("头等舱:Y2400<br/>商务舱:Y2200");
				
			}else{
				
				w.println("头等舱:Y1800<br/>商务舱:Y1600");
				
				
			}
			
			
			
		}
		
		
		

	}
	
	
	
	
}















